package com.miyuan.wm.utils;

import com.miyuan.wm.enums.ExceptionCodeEnum;
import com.miyuan.wm.response.BaseResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.Map;

public class MiscUtils {

	static public BaseResponse<Map<String, String>> getValidateError(BindingResult bindingResult) {
		if (bindingResult.hasErrors() == false)
			return null;
		Map<String, String> fieldErrors = new HashMap<String, String>();
		for (FieldError error : bindingResult.getFieldErrors()) {
			fieldErrors.put(error.getField(), error.getDefaultMessage());
		}
		BaseResponse<Map<String, String>> ret = new BaseResponse<Map<String, String>>(
				ExceptionCodeEnum.REQUEST_PARAM_ERROR);
		ret.setErrMsg(fieldErrors.toString());
		ret.setData(fieldErrors);
		return ret;
	}

}